import React, { Component } from 'react';
import PropTypes from 'prop-types';
import LazyLoad from 'react-lazyload';
import './index.css';
import TokenAPI from '../../../api/auth/index';
import { message } from 'antd';
import { WEB_DOMAIN } from '../../../utils/constants';

class Application extends Component {

    componentDidMount() {
        const newsharedBy = this.props.sharedBy;
        const newshareId = this.props.shareId;
        const newNickName = this.props.nickTitleName;
        const nickTitle = this.props.nickTitle;
        const nickPic = this.props.nickPic;
        this.getShare().then((res) => {
            if (res.code === 1) {
                wx.config({
                    debug: false,
                    appId: res.data.appId,
                    timestamp: res.data.timestamp,
                    nonceStr: res.data.nonceStr,
                    signature: res.data.signature,
                    jsApiList: [
                        'onMenuShareTimeline', //分享给好友
                        'onMenuShareAppMessage', //分享到朋友圈
                    ]
                });


                wx.ready(function () {
                    // console.log("ready");
                    //分享到朋友圈
                    wx.onMenuShareTimeline({
                        title: `${newNickName}`, // 分享标题
                        // link:location.href.split('#')[0],
                        link: `${WEB_DOMAIN}/fileShareMobileWX/${newshareId}/${newsharedBy}`,
                        imgUrl: `${nickPic}`,// 分享图标
                        success: function () {
                            // alert('已分享')
                        },
                        cancel: function () {
                            // alert('已取消')
                        }
                    });

                    //分享给朋友
                    wx.onMenuShareAppMessage({
                        title: `${newNickName}`, // 分享标题
                        desc: `${nickTitle}`, // 分享描述
                        // link:location.href.split('#')[0],
                        link: `${WEB_DOMAIN}/fileShareMobileWX/${newshareId}/${newsharedBy}`,
                        imgUrl: `${nickPic}`, // 分享图标
                        type: '', // 分享类型
                        dataUrl: '',
                        success: function () {
                            // alert('已分享')
                        },
                        cancel: function () {
                            // alert('已取消')
                        }
                    });
                });

                wx.error(function () {
                    message.warning('分享失效');
                });
            }
        });
    }

    getShare = async () => {
        const newsharedBy = this.props.sharedBy;
        const newshareId = this.props.shareId;
        // const shareURL = `${WEB_DOMAIN}/fileShareMobile/${newshareId}/${newsharedBy}`;
        const shareURL = encodeURIComponent( location.href.split('#')[0] );
        const response = await TokenAPI.getWxShare(shareURL);
        const { code, data } = response;
        if (code === 1) {
            return response;
        }
    }


    render() {
        const telphone= this.props.telephone;
        var availWidth = window.screen.availWidth;
        return (
            <div>
                {/*<div className="titlePanel">{this.props.nickName}</div>*/}
                <a href={`tel:${telphone}`} className='imgBak'><img src='/images/icon_call.png' alt=""/></a>
                <div className="widget-list image-container">
                    {
                        this.props.imgList.map((item) =>{
                            return (
                                <LazyLoad key={item} throttle={200} height={200}>
                                    <img style={{ width: availWidth-22 }} src={item} alt="" />
                                </LazyLoad>
                            );
                        })
                    }
                </div>
            </div>
        );
    }

}

Application.propTypes = {
    imgList: PropTypes.array.isRequired,
    sharedBy:PropTypes.string.isRequired,

};

export default Application;
